package heiing.collection.map;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.Getter;

/**
 *
 * @author heiing
 * @param <K>
 * @param <V>
 */
@Getter
public class MapDiffResult<K, V> {

    /**
     * 新增的 K-V
     */
    private final Map<K, V> newItems;

    /**
     * 已删除的 K-V
     */
    private final Map<K, V> removedItems;

    /**
     * 已变更的 K-V。Changed 存储变更前后的值
     */
    private final Map<K, Changed<V>> changedItems;

    public MapDiffResult() {
        this(null, null, 0);
    }

    public MapDiffResult(Map<K, V> adds, Map<K, V> removed) {
        this(adds, removed, 0);
    }

    public MapDiffResult(Map<K, V> adds, Map<K, V> removed, int changeCapacity) {
        this.newItems = null == adds ? Collections.emptyMap() : adds;
        this.removedItems = null == removed ? Collections.emptyMap() : removed;
        this.changedItems = new LinkedHashMap<>(changeCapacity);
    }

    public final MapDiffResult<K, V> putChanged(K key, V a, V b) {
        this.changedItems.put(key, new Changed<>(a, b));
        return this;
    }

    @Getter
    public static class Changed<V> {
        /**
         * Value A，变更前的值（第一个参数的值）
         */
        private final V valueA;
        /**
         * Value B，变更后的值（第二个参数的值）
         */
        private final V valueB;

        private Changed(V a, V b) {
            this.valueA = a;
            this.valueB = b;
        }
    }

}
